package com.songcha.sakura.mvvm.comic.detail

import com.songcha.library_base.mvvm.base.BaseRepository
import com.songcha.sakura.bean.ComicDetailBean
import com.songcha.sakura.config.BaseConfig
import io.reactivex.Observable
import org.json.JSONObject
import org.jsoup.Jsoup

class ComicDetailRepository : BaseRepository()  {


    fun getComicDetail(json: JSONObject?): Observable<ComicDetailBean> {
        return createNetworkObservable {
            //val data= mutableListOf<ComicListBean.ComicBean>()

            var href = ""
            if (json != null) {
                if (json.has("href"))
                    href = json.getString("href")

            }

            val document = Jsoup.connect("${BaseConfig.BASE_URL}$href").get()

            val xs = document.getElementsByClass("col-xs-1")[2]

            val thumb = xs.getElementsByClass("lazyload").attr("data-original")

            val content = xs.getElementsByClass("myui-content__detail")[0]

            val title = content.getElementsByClass("title")[0].text()
            val score = content.getElementsByClass("branch")[0].text()
            val cate = content.selectFirst("p[class=data]")!!.getElementsByTag("a")[0].text()
            val area = content.selectFirst("p[class=data]")!!.getElementsByTag("a")[1].text()
            val year = content.selectFirst("p[class=data]")!!.getElementsByTag("a")[2].text()
            val update = content.select("p[class=data hidden-sm] span[class=text-red]").text()

            val actorsElement = content.select("p[class=data]")[1].getElementsByTag("a")
            val actors = arrayListOf<String>()
            if (actorsElement.size > 0) {
                for (e in actorsElement) {
                    actors.add(e.text())
                }
            } else {
                actors.add("未知")
            }

            val directorElement = content.select("p[class=data]")[2].getElementsByTag("a")
            var director = "未知"
            if (directorElement.size > 0) {
                director = directorElement[0].text()
            }

            var brief = content.select("p[class=data hidden-xs]").text()
            brief = brief.substring(3, brief.length - 2)

            val briefAll = document.getElementsByClass("sketch content").text()

            val play_source_element =
                document.getElementsByClass("nav nav-tabs active")[0].getElementsByTag("li")
            val play_source = arrayListOf<String>()
            for (e in play_source_element) {
                play_source.add(e.text())
            }

            val play_url_list_element =
                document.getElementsByClass("tab-content myui-panel_bd")[0].getElementsByTag("ul")

            val playurlList: MutableList<MutableList<ComicDetailBean.EpisodeBean>> = mutableListOf()

            for (e in play_url_list_element) {
                val lie = e.getElementsByTag("li")
                val list: MutableList<ComicDetailBean.EpisodeBean> = mutableListOf()
                for (e2 in lie) {
                    val a = e2.getElementsByTag("a")[0]
                    val episode = ComicDetailBean.EpisodeBean(a.text(), a.attr("href"))
                    list.add(episode)
                }
                playurlList.add(list)
            }

            val bean=ComicDetailBean(
                thumb,
                title,
                score,
                cate,
                area,
                year,
                update,
                actors,
                director,
                brief,
                briefAll,
                play_source,
                playurlList,
                ""
            )

            it.onNext(bean)
            it.onComplete()
        }


        //return RetrofitClient.api.getHomeCartList().await()
    }



}